(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev.hourana')
        .controller('HouranaListCtrl', HouranaListCtrl);

    /** @ngInject */
    function HouranaListCtrl($scope, $filter, $timeout, HouranaService) {
        $scope.houranalist = [];

        var nowDate = new Date();
        $scope.startDate = nowDate.setDate(nowDate.getDate() - 7);
        $scope.format = "yyyy-MM-dd";
        $scope.altInputFormats = ['yyyy-M!-d!'];
        $scope.popup1 = {
            opened: false
        };

        $scope.itemArray= [];
        $scope.showSensor = false;

        console.log($scope.startDate);

        $scope.open = function() {
            $scope.popup1.opened = true;
        };

        $scope.endDate = (new Date()).getTime();
        $scope.popup2 = {
            opened: false
        };

        $scope.open2 = function() {
            $scope.popup2.opened = true;
            console.log($scope.startDate);
            console.log($scope.selected);
            console.log($scope.checkboxes.items);
        };

        $scope.LoadPage = function (tableState) {
        };

        $scope.checkboxes = {
            checked: false,
            items: {}
        };

        $scope.selectChnList = [];
        $scope.selected = { value: $scope.itemArray[0] };
        $scope.chnIds = [];

        $scope.$watch('selected.value', function (value, oldValue) {
            if (value) {
                console.log(value);
                HouranaService.getAnaChnByType({type: value['id']}, function (data) {
                    console.log(data.result);
                    $scope.selectChnList = data.result;
                    $scope.checkboxes.items = {};

                    $timeout($scope.bindCheckBox, 500);
                })
            }
        });

        $scope.$watch('checkboxes.items', function () {
            var chnIds = [];
            for (var key in $scope.checkboxes.items) {
                if ($scope.checkboxes.items[key]) {
                    chnIds.push(key);
                }
            }
            $scope.chnIds = chnIds;
        }, true);

        $scope.$on('$viewContentLoaded', function() {
            HouranaService.getSensors({}, function (data) {
                console.log(data.result);
                $scope.itemArray= [];
                angular.forEach(data.result, function (chn) {
                    var item = {
                        id: chn['type'],
                        name: chn['name']
                    };
                    $scope.itemArray.push(item);
                });

                $scope.showSensor = true;
            });
        });
    }

})();
